$(function () {
    // 是否重复打开菜单
    let reOpenMenu = false;
    //加载弹出层
    layui.use(['element'],
        function () {
            layer = layui.layer;
            element = layui.element;
        });
    $('.x_left_open').click(function (event) {
        // noinspection JSJQueryEfficiency
        if ($('.layui-side').css('left') === '0px') {
            $('.layui-side').animate({left: '-200px'}, 100);
            $('.layui-body').animate({left: '0px'}, 100);
        } else {
            $('.layui-side').animate({left: '0px'}, 100);
            $('.layui-body').animate({left: '200px'}, 100);
        }
    });
    //触发事件
    var active = {
        tabAdd: function (title, url, id) {
            if (!reOpenMenu) {
                // noinspection JSJQueryEfficiency
                for (var i = 0; i < $('.tab-iframe').length; i++) {
                    // noinspection EqualityComparisonWithCoercionJS
                    if ($('.tab-iframe').eq(i).attr('tab-id') == id) {
                        this.tabChange(id);
                        event.stopPropagation();
                        return;
                    }
                }
            }
            //新增一个Tab项
            element.tabAdd('xbs-table', {
                title: title,
                content: '<iframe tab-id="' + id + '" frameborder="0" src="' + url + '" scrolling="yes" class="tab-iframe"></iframe>',
                id: id
            })
        }, tabChange: function (id) {
            //切换到指定Tab项
            element.tabChange('xbs-table', id); //切换到：用户管理
        }
    };

    $('.menu').click(function (event) {
        var url = $(this).attr('_href');
        var title = $(this).html();
        var index = $('.layui-nav-tree li a').index($(this));
        active.tabAdd(title, url, index);
        active.tabChange(index);
        event.stopPropagation();
    });
});